package com.woniuxy.microretailserver.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microretailserver.bean.GoodsOrderBean;
import com.woniuxy.microretailserver.bean.GoodsOrderExportBean;
import com.woniuxy.microretailserver.bean.PageRequest;
import com.woniuxy.microretailserver.bean.ResponesMsg;
import com.woniuxy.microretailserver.service.IGoodsOrderService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/**
 * @author chenhu
 * 商品订单控制层
 */
@RestController
@RequestMapping("/goods")
public class GoodsOrderController {

    @Resource
    private IGoodsOrderService goodsOrderServiceImpl;

    /**
     * 导出商品订单数据
     * @throws Exception
     */
    @GetMapping(value = "/order",produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveOrder(HttpServletResponse response) throws Exception {
        //获取数据
        List<GoodsOrderExportBean> byAllOrder = goodsOrderServiceImpl.findByAllOrder();
        //导出excel
        //参数1：exportParams导出配置对象,参数2:导出的类型 参数3:导出的数据集合
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("订单信息表","订单信息"),GoodsOrderExportBean.class,byAllOrder);
        //将excel写入指定的位置
        response.setHeader("content-disposition","attachment;fileName=" + URLEncoder.encode("订单信息.xls","UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.close();
    }

    /**
     * 展示终端销售单
     * @param request
     * @return
     */
    @RequestMapping(value = "/findGoodsOrder",produces = {"application/json;charset=utf-8"})
    public PageInfo<GoodsOrderBean> findGoodsOrder(PageRequest request){
        GoodsOrderBean goodsOrderBean = new GoodsOrderBean();
        PageInfo<GoodsOrderBean> allGoodsOrder = goodsOrderServiceImpl.findAllGoodsOrder(request, goodsOrderBean);
        return allGoodsOrder;
    }

    /**
     * 模糊查询终端销售订单数据
     * @param request
     * @param
     * @return
     */
    @GetMapping(value = "/findByModule",produces = {"application/json;charset=utf-8"})
    public PageInfo<GoodsOrderBean> findByModule(PageRequest request,String orderNo){
        System.out.println(orderNo);
        GoodsOrderBean goodsOrderBean = new GoodsOrderBean();
        goodsOrderBean.setOrderNo(orderNo);
        PageInfo<GoodsOrderBean> allGoodsOrder = goodsOrderServiceImpl.findAllGoodsOrder(request, goodsOrderBean);
        return allGoodsOrder;
    }

    /**
     * 根据订单号查询详细信息,终端销售单详情
     * @param orderNo
     * @return
     */
    @GetMapping(value = "/findNo",produces = MediaType.APPLICATION_JSON_VALUE)
    public HashMap<String, Object> findAll(String orderNo){
        HashMap<String, Object> all = goodsOrderServiceImpl.findAll(orderNo);
        System.out.println(all);
        return all;
    }
}
